<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="http://libs.useso.com/js/html5shiv/3.7/html5shiv.min.js"></script>
    <script type="text/javascript" src="http://libs.useso.com/js/respond.js/1.4.2/respond.min.js"></script>
    <script type="text/javascript" src="http://cdn.bootcss.com/css3pie/2.0beta1/PIE_IE678.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="../../static/h-ui/css/H-ui.min.css"/>
    <link rel="stylesheet" type="text/css" href="../../static/h-ui.admin/css/H-ui.admin.css"/>
    <link type="text/css" rel="stylesheet" href="../../static/font/font-awesome.min.css"/>
    <link href="../../css/easyui/icon.css" rel="stylesheet" type="text/css"/>
    <link href="../../css/easyui/default/easyui.css" rel="stylesheet" type="text/css"/>
    <!--[if IE 7]>
    <link href="http://www.bootcss.com/p/font-awesome/assets/css/font-awesome-ie7.min.css" rel="stylesheet"
          type="text/css"/>
    <![endif]-->
    <title>添加用户</title>
</head>
<body>
<div class="pd-20">
    <div class="Huiform">
        <form action="" method="post" id="formData">
            <input type="hidden" name="dpId" id="dpId">
            <table class="table table-bg">
                <tbody>
                <tr>
                    <th width="100" class="text-r"><span class="c-red">*</span>部门名称：</th>
                    <td><input type="text" style="width:200px" class="input-text" value="" placeholder=""
                               id="deptName" name="deptName" datatype="*2-16" nullmsg="部门名称不能为空"></td>
                </tr>
                <tr>
                    <th width="100" class="text-r"><span class="c-red">*</span> 部门ID：</th>
                    <td><input type="text" style="width:200px" class="input-text" value="" placeholder="" id="deptId"
                               name="deptId" datatype="*2-16" nullmsg="部门ID不能为空"></td>
                </tr>
                <tr>
                    <th width="100" class="text-r"><span class="c-red">*</span> 父级部门：</th>
                    <td>
                        <input type="text" style="width:200px" class="easyui-combotree" value="" placeholder="" id="showId"
                               name="shouwId" datatype="*2-16">
                        <input type="hidden" name="parId" id="parId">
                    </td>

                </tr>
                <th></th>
                <td></td>
                </tr>
                </tbody>
            </table>
        </form>
        <button class="btn btn-success radius" type="" id="save"><i class="icon-ok"></i> 确定</button>
    </div>
</div>
<script type="text/javascript" src="../../lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="../../lib/jquery/1.9.1/jquery.js"></script>
<script type="text/javascript" src="../../lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../../js/Validform_v5.3.2_min.js"></script>
<script type="text/javascript" src="../../h-ui/js/H-ui.js"></script>
<script type="text/javascript" src="../../h-ui.admin/js/H-ui.admin.js"></script>

<script type="text/javascript">
    // $(".Huiform").Validform();
</script>
<script>
    $(function () {
       // $('#showId').combotree();

        if (getQueryVariable("pid") != "false") {
            var pid = getQueryVariable("pid");
            var jsonArray = [];
            if (pid != null && pid != "") {
                var obj = {'column': "dp_id", 'type': 'eq', "value": pid};
                jsonArray.push(obj)
            }
            $.ajax({
                type: 'POST',
                url: '/department/find',
                dataType: 'json',
                data: {paramData: JSON.stringify(jsonArray)},
                success: function (data) {
                    //alert(data.data.toString());
                    cpJsonToForm(data.data, "formData");
                    comboTree();
                }
            })


        }
    });


    $("#save").click(function () {
        var t = $('#showId').combotree('tree');	// get the tree object
        var n = t.tree('getSelected');		// get selected node
        if (n!=null) {
            //  alert(n.id);
            $("#parId").val(n.id);
        }
        var dpId = $("#dpId").val();
        if (dpId != null && dpId != "") {
            save(1);
        } else {
            save(0);
        }

    })

    function save(type) {
        var url;
        if (type == 0) {
            url = '/department/add';
        } else {
            url = '/department/update';
        }
        var formObject = {};
        var formArray = $("#formData").serializeArray();
        $.each(formArray, function (i, item) {
            formObject[item.name] = item.value;
        });
        //  alert(JSON.stringify(formObject));
        $.ajax({
            url: url,
            type: "post",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formObject),
            dataType: "json",
            success: function (data) {
                alert("保存成功！");
                window.opener.loadlist();
                window.close();
            },
            error: function () {
                console.log("提交ajax函数异常");
            },

        });
    }

    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return "flase";
    }

    var cpJsonToForm = function (json, formId) {
        var serializeArray = $("#" + formId).serializeArray();
        for (var i = 0; i < serializeArray.length; i++) {
            var name = serializeArray[i].name;
            if (json[name]) {
                var value = json[name];
                $("[name='" + name + "']").val(value);
            }
        }
    };

    function comboTree() {
        $.ajax({
            url: "/common/selectDept",
            type: "POST",
            dataType: "json",
            success: function (data) {
                $('#showId').combotree(
                    'loadData',
                    eval(data.msg));
                var checkV = ( $("#parId").val());
                if (checkV!=null && checkV!=""){
                    $('#showId').combotree('setValue', checkV);
                }

            },
            error: function () {
                alert("error")
            }
        });
    }
</script>
</body>
</html>
